package homework.james.xiao.bean;

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class Waiter {
	private OrdersHistory ordersHistory;
	private Orders orders;
	private Menu menu;
	private PrintWriter ps;
	
	public Waiter(PrintWriter ps) throws FileNotFoundException{
		menu = Menu.getInstance();
		orders = new Orders();
		ordersHistory = OrdersHistory.getInstance();
		this.ps = ps;
	}
	public Waiter() throws FileNotFoundException{
		menu = Menu.getInstance();
		orders = new Orders();
		ordersHistory = OrdersHistory.getInstance();
		this.ps = new PrintWriter(System.out, true);
	}

	/**
	 * 将当天的订单记录到历史记录中
	 * 
	 * @author James Xiao  
	 * @datetime Sep 18, 20092:50:51 PM
	 */
	public void saveOrder(){
		if (this.orders.getOrders().size()>0){
			this.ordersHistory.addOrders(orders);
			ordersHistory.writeToXML();
			orders = new Orders();
			ps.println("your order was saved");
		}else
			ps.println("your oders is empty, please book first");
			
	}
	/**
	 * 打印菜单
	 * @throws FileNotFoundException
	 * @author James Xiao  
	 * @datetime Sep 18, 20092:51:20 PM
	 */
	public void printMenu() throws FileNotFoundException{
		menu.print(ps);
	}
	/**
	 * 为name订购序号为number的dish
	 * @param number
	 * @param name
	 * @return
	 * @author James Xiao  
	 * @datetime Sep 18, 20092:51:28 PM
	 */
	public boolean bookDish(String number, String name){
		User u = new User (name);
		int d =  Integer.valueOf(number)-1;
		if (d>=menu.getItems().size())
			return false;
		else{
			Dish dish = menu.getItems().get(d);
			orders.addOrder(new Order(u,dish));
			ps.println(dish.getName() + " was booked for " + name);
			return true;
		}
	}
	/**
	 * 显示本次订单
	 * 
	 * @author James Xiao  
	 * @datetime Sep 18, 20092:51:52 PM
	 */
	public void printOrders(){
		orders.print(ps,true,true);
	}
	/**
	 * 显示历史订单
	 * 
	 * @author James Xiao  
	 * @datetime Sep 18, 20092:52:07 PM
	 */
	public void printOrdersHistroy(){
		ordersHistory.print(ps);
	}
	/**
	 * 显示帮助信息
	 * 
	 * @author James Xiao  
	 * @datetime Sep 18, 20092:52:16 PM
	 */
	public void printHelp(){
		ps.println("Usage:");
		ps.println("menu\t\t\t\t|get the menu");
		ps.println("book [number] for [username]\t|book dished for user");
		ps.println("history\t\t\t\t| browse orders history");
		ps.println("orders\t\t\t\t| browse current orders");
		ps.println("save\t\t\t\t| save current orders");
		ps.println("exit\t\t\t\t| exit the system");
	}
	/**
	 * 处理用户命令
	 * @param command
	 * @return
	 * @throws FileNotFoundException
	 * @author James Xiao  
	 * @datetime Sep 18, 20092:52:25 PM
	 */
	public boolean handle(String command) throws FileNotFoundException{
		if(command.equalsIgnoreCase("menu")){
			printMenu();
		}else if(command.equals("history")){
			printOrdersHistroy();
		}else if (command.equals("orders")){
			printOrders();
		}else if (command.equals("help")){
			printHelp();
		}else if (command.startsWith("book")){
			//TODO 对错误输入的检测不够全面
			if(command.split(" ").length!=4){
				ps.println("Wrong args for book command");
			}else				
			if (bookDish(command.split(" ")[1],command.split(" ")[3]))
				ps.println("your dish was booked");
			else
				ps.println("there isn't any dish as requested");
		}else if(command.equals("exit")){
			return true;
		}else if(command.equals("save")){
			saveOrder();
		}else {
			ps.println("Invalid Command");
			printHelp();
		}
		return false;
	}
	
	//TODO 删除本次订单中的某些订购项
	//TODO 清空本次订单
	//TODO 用户退出时检测本次订单是否已经保存
}
